/*
 * Copyright 2010 WithOne Co. Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.co.withone.osgi.gadget.picasaconnector;

import java.util.ResourceBundle;

import jp.co.withone.osgi.gadget.bundlecontroller.viewer.SettingViewListener;
import jp.co.withone.osgi.gadget.pictureviewer.PictureController;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

/**
 * 
 * @author ohsoko
 */
public class PicasaConnectorActivator implements BundleActivator,
        SettingViewListener {

    public static final ResourceBundle RB = ResourceBundle
            .getBundle("jp.co.withone.osgi.gadget.picasaconnector.resource.message");

    private ServiceRegistration pictureControllerRegistration = null;
    private ServiceRegistration settingViewListenerRegistration = null;
    private PicasaConnectorSettingView settingView = null;
    private PicasaConnector connector = null;
    public boolean isStopped = true;

    private Object lock = new Object();

    public void start(BundleContext context) throws Exception {

        synchronized (this.lock) {

            this.connector = PicasaConnector.getInstance();

            // register SettingViewListener Service
            settingViewListenerRegistration = context.registerService(
                    SettingViewListener.class.getName(), this, null);
            
            // register PictureController Service
            pictureControllerRegistration = context.registerService(
                    PictureController.class.getName(),
                    new PictureControllerImpl(this.connector), null);

            this.isStopped = false;
        }
    }

    public void stop(BundleContext context) throws Exception {

        synchronized (this.lock) {
            this.isStopped = true;

            if (this.settingView != null) {
                this.settingView.dispose();
                this.settingView = null;
            }
            
            // unregister PictureController Service
            if (this.pictureControllerRegistration != null) {
                this.pictureControllerRegistration.unregister();
                this.pictureControllerRegistration = null;
            }

            // unregister SettingViewListener Service
            if (this.settingViewListenerRegistration != null) {
                this.settingViewListenerRegistration.unregister();
                this.settingViewListenerRegistration = null;
            }

            this.connector = null;
        }
    }

    @Override
    public void show(int x, int y) {
        synchronized (this.lock) {
            if (this.isStopped) {
                return;
            }
            if (this.settingView == null) {
                this.settingView = new PicasaConnectorSettingView(this.connector);
                this.settingView.setBounds(x, y, this.settingView.getWidth(),
                        this.settingView.getHeight());
            }
            if (!this.settingView.isVisible()) {
                this.settingView.setVisible(true);
            }
        }
    }
}
